package com.fushenlan.commons;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.CodeSignature;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author PC
 * @title: AspectUtils
 * @projectName SpringBootDemo
 * @description: TODO
 * @date 2022/1/20 16:59
 */
public class AspectUtils {
    public AspectUtils() {

    }

    public static Map<String, Object> getParamMap(ProceedingJoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        CodeSignature codeSignature = (CodeSignature) joinPoint.getStaticPart().getSignature();
        String[] paramNames = codeSignature.getParameterNames();
        if (paramNames != null && paramNames.length != 0) {
            Map<String, Object> methodParaMap = new HashMap<>(paramNames.length);
            for (int i = 0; i < paramNames.length; ++i) {
                methodParaMap.put(paramNames[i], args[i]);
            }
            return methodParaMap;
        } else {
            throw new RuntimeException("Can't get parameter names !");

        }

    }

    public static Method getMethod(ProceedingJoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        return methodSignature.getMethod();

    }
}
